package com.sjgs.common.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.sjgs.common.Constants.PATTERN_TILESET;
import static com.sjgs.common.Constants.PATTERN_TMS;

public class DirUtils {

    public static String dir2Path(String dir) {
        String path = dir.replaceAll("\\\\|/|//", ".").trim();
        path = path.replaceAll("\\.{2,}", ".");
        if (path.startsWith(".")) {
            path = path.substring(1, path.length());
        }
        if (path.endsWith(".")) {
            path = path.substring(0, path.length() - 1);
        }
        return path;
    }

    public static String getFileDir(String path) {
        String filepath = path.replaceAll("\\\\|//", "/").trim();
        if (filepath.indexOf("/") > -1) {
            return filepath.substring(0, filepath.lastIndexOf("/") - 1);
        } else {
            return "/";
        }
    }

    public static String getDir(String dir) {
        return (dir.length() > 1) ? (dir + "/") : dir;
    }

    public static boolean mkDir(String dir) {
        Path dirPath = Paths.get(dir);
        if (StringUtils.isBlank(dir) || dir.length() < 2 || Files.exists(dirPath)) {
            return false;
        }
        try {
            Files.createDirectories(dirPath);
            return true;
        } catch (IOException e) {
            ;
        }
        return false;
    }

    public static String getFileDir(String rootDir, String dir, String fkey) {
        return rootDir + getDir(dir) + fkey;
    }

    public static Path getChunkPath(String rootDir, String dir, String fkey, Long index) {
        return Paths.get(getFileDir(rootDir, dir, fkey) + "/" + index);
    }

    public static String getTilePath(String rootDir, String url) {
        url = url.replaceAll("\\\\|//", "/").trim();
        Pattern pattern = Pattern.compile(PATTERN_TMS);
        Matcher matcher = pattern.matcher(url);
        if (matcher.find()) {
            return rootDir + "/"
                    + matcher.group(1) + "/"
                    + matcher.group(2) + "/"
                    + matcher.group(3) + matcher.group(4);
        }
        return null;
    }

    public static String getTileSetMetaPath(String rootDir, String url) {
        url = url.replaceAll("\\\\|//", "/").trim();
        Pattern pattern = Pattern.compile(PATTERN_TILESET);
        Matcher matcher = pattern.matcher(url);
        if (matcher.find()) {
            return rootDir + "/" + matcher.group(1) + matcher.group(2);
        }
        return null;
    }

    public static String getTileRootPath(String rootDir, String url) {
        url = url.replaceAll("\\\\|//", "/").trim();
        return rootDir + url.substring(url.lastIndexOf("/"), url.length());
    }

    public static void main(String[] args) {
        String url = "E:\\gugong\\tileset\\layer.json";
        //System.out.println(getTilePath("bk8kfi53quajr", url));
        System.out.println(getTileRootPath("bk8kfi53quajr", url));
    }
}
